﻿/* Copyright (c) Microsoft Corporation
 * 
 * All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.  You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 * 
 * See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProximityTapper
{
    /// <summary>
    /// Static class for providing helper functions to parse a message
    /// </summary>
    [CLSCompliant(false)]
    public class MessageParser
    {
        /// <summary>
        /// This would try to parse Message Data. 
        /// </summary>
        /// <param name="data">Message Data</param>
        /// <param name="messageType">Message Type</param>
        /// <returns>IMessageData object if successfully parsed</returns>
        public static IMessageData TryParse(byte[] data, string messageType)
        {
            IMessageData iMessage = null;

            // Check if message type is Service Descriptor
            if (0 == messageType.CompareTo("Windows.windows.com/SD"))
            {
                // Try parsing ServiceDescriptor message
                iMessage = new ServiceDescriptor();

                if (iMessage.TryParse(data))
                {
                    // Return this ServiceDescriptor message
                    return iMessage;
                }
                else
                {
                    // This should never happen as message type for SD is known
                    // and we should successfully parse the message.
                    System.Diagnostics.Debug.Assert(false);
                }
            }
            else
            {
                // Else try parsing OobConnector message
                iMessage = new OobConnector(false);

                if (iMessage.TryParse(data))
                {
                    // Return this OobConnector message
                    return iMessage;
                }

                // Else try parsing OobConnectorAck message
                iMessage = new OobConnector(true);

                if (iMessage.TryParse(data))
                {
                    // Return this OobConnectorAck message
                    return iMessage;
                }

                // Else try parsing SessionFactoryServiceActivation message
                iMessage = new SessionFactoryServiceActivation();

                if (iMessage.TryParse(data))
                {
                    // Return this SessionFactoryServiceActivation message
                    return iMessage;
                }

                // Else try parsing SessionActivation message
                iMessage = new SessionActivation(false);

                if (iMessage.TryParse(data))
                {
                    // Return this SessionActivation message
                    return iMessage;
                }

                // Else try parsing SessionActivationAck message
                iMessage = new SessionActivation(true);

                if (iMessage.TryParse(data))
                {
                    // Return this SessionActivationAck message
                    return iMessage;
                }
            }

            // If we made it to here, then we were unable to parse the data, 
            // simply create a byte message data
            return new MessageByteData(data);
        }
    }
}
